<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of soaUsers
 *
 * @author dxx
 */

namespace app\common\model\activity;
use think\Model;

class CouponsCoup extends Model {

    private   $_companyId = null;
    protected $table = 'coupons_coup';
    protected $connection = ['database' => 'activity'];

    /**
     * 构造方法
     * @param type $data
     */
    public function __construct($data=[]) {
        parent::__construct($data);
    }
    
    
    private function _createTable($companyId){
        $this->_companyId = $companyId;
        $this->execute('CREATE TABLE IF NOT EXISTS ' . "coupons_coup_{$companyId}" . ' LIKE  coupons_coup'); //没有则新建表
    }
    
    /**
     * 通过本卡券的唯一标识判断是否已经领取过了卡券
     * @param type $couponsId
     * @param type $companyId
     * @param type $uniqueId
     * @return type
     */
    public function getCouponsByUniqueId($couponsId,$companyId,$uniqueId){
        $this->_createTable($companyId);
        $coupons = $this->setTable("coupons_coup_{$companyId}")->where([
            "unique_id" => "$uniqueId",
            "coupon_id" => "$couponsId",
        ])->find();
        
        return $coupons;
    }
    
    /**
     * 通过本卡券的唯一标识判断是否已经批量领取过了卡券
     * @param type $couponsId
     * @param type $companyId
     * @param type $uniqueId
     * @return type
     */
    public function getBatchCouponsByUniqueId($couponsId,$companyId,$uniqueId){
        $this->_createTable($companyId);
        $coupons = $this->setTable("coupons_coup_{$companyId}")->where([
            "unique_id" => "$uniqueId",
            "coupon_id" => "$couponsId",
        ])->select();
        
        return $coupons;
    }

    /**
     * 获取某个人在一个活动领取了多少张券
     * @param type $mobile
     * @param type $couponsId
     * @param type $companyId
     * @return type
     */
    public function getReceiveTotal($mobile, $couponsId, $companyId) {
        
        $this->_createTable($companyId);
        
        return $this->setTable("coupons_coup_{$companyId}")->where([
            "mobile" => $mobile,
            "coupon_id" => $couponsId,
        ])->count();
    }
    
    /**
     * 根据openid获取叽叽券(领到的券)
     * @param type $companyId
     * @param type $openId
     * @param type $status
     * @param type $page
     * @param type $pageSize
     * @return type
     */
    public function getCouponsListByOpenId($companyId, $openId, $status,$page=0,$pageSize=0) {
        
        $this->_createTable($companyId);
        
        //更新券状态
        $where = [
                'open_id'  => $openId,
                'status'   => 0,
                'end_date' => ['elt',time()],  
        ];
        $this->setTable("coupons_coup_{$companyId}")->where($where)->update(['status' => 2]);
        //更新券状态完成
        //需要输出的字段
        $columns = array(
            "id",
            "coupon_id",
            "sn",
            "status",
            "start_date",
            "end_date",
            "use_date",
            "store_id",  
        );
        //分页条件
        $page = $page < 1 ? 1 : $page;
        $pageSize = $pageSize < 1 ? 4 : $pageSize;
        $pageSize>50?$pageSize = 50:'';
        
        $condition= [
            'open_id'=> $openId,
            "status" => $status
        ];
        
        $couponsList = $this->setTable("coupons_coup_{$companyId}")->field($columns)
                ->where($condition)->order('id DESC')->page($page,$pageSize)->select();
        
        return $couponsList;
    }
    
    
    /**
     * 根据VIPID获取叽叽券
     * @param type $companyId
     * @param type $vipId
     * @param type $status
     * @param type $page
     * @param type $pageSize
     * @return type
     */
    public function getMyCouponsByVipId($companyId, $vipId, $status,$page =0,$pageSize=0) {
        
        $this->_createTable($companyId);
        
        //更新券状态
        $where = [
                'vip_id'   => $vipId,
                'status'   => 0,
                'end_date' => ['elt',time()], 
        ];
        
        $ret = $this->setTable("coupons_coup_{$companyId}")->where($where)->update( ['status' => 2]);

        //更新券状态完成
        //需要输出的字段
        $columns = array(
            "id",
            "coupon_id",
            "sn",
            "status",
            "start_date",
            "end_date",
            "use_date",
            "store_id",            
        );
        //分页条件
        $page = $page < 1 ? 1 : $page;
        $pageSize = $pageSize < 1 ? 4 : $pageSize;
        $start = ($page - 1) * $pageSize;
        
        $condition = [
                'vip_id' => $vipId,
                "status" => $status
        ];
        
        $couponsList = $this->setTable("coupons_coup_{$companyId}")->field($columns)
                ->where($condition)->order('id DESC')->page($page,$pageSize)->select();
        
        return $couponsList;
    }
    
    
    /**
     * 获取卡券信息
     * @param type $couponId 卡券id
     * @param type $companyId 公司id
     */
    public function getCouponCoupInfoById($couponId, $companyId){
        
        $this->_createTable($companyId);
        $where =  ['id' => $couponId];
        $info = $this->setTable("coupons_coup_{$companyId}")->where($where)->find();
        return $info;
    }
    
    
    /**
     * 根据sn获取领券表的记录
     * @param type $companyId
     * @param type $sn
     * @return type
     */
    public function getCouponCoupInfoBySn($companyId,$sn){
        $this->_createTable($companyId);
        $where = ['sn'=>$sn];
        $info = $this->setTable("coupons_coup_{$companyId}")->where($where)->find();
        return $info;
    }
    
    /**
     * 更新CouponCoupXx表的记录
     * @param type $companyId  公司id
     * @param type $couponCoupId 领取卡券id
     * @param type $updateData   要更新的数据
     */
    public function updateCouponCoupById($companyId,$couponCoupId,$updateData){
        $this->_createTable($companyId);
        $where =  ['id' => $couponCoupId];
        
        $data = array(
            'status'   => $updateData['status'],
            'staff_id' => $updateData['staff_id'],
            'use_date' => $updateData['use_date'],
            'store_id' => $updateData['store_id']
        );
        
        return  $this->setTable("coupons_coup_{$companyId}")->where($where)->update($data);
    }
    
    /**
     * 根据openId更新叽叽券(领到的券)
     * @param type $companyCode
     * @param type $openId
     * @param type $vipId
     * @return boolean
     */
    public function updateMyCouponsByOpenId($companyId, $openId, $vipId,$mobile) {
        
        $this->_createTable($companyId);
        //更新
        $condition = [
            'open_id' => $openId,
            "mobile"  => $mobile
        ];
        
        $ret = $this->setTable("coupons_coup_{$companyId}")->where($condition)->update(['vip_id' => $vipId]);
        return $ret;
    }
}